import base64
from souti import settings

def image2base64(path):
    path1=path
    try:
        path = settings.BASE_DIR + path
        i = path.rfind('.')
        image_type = path[i + 1:]
        img = open(path, "rb")
        data = img.read()
        img.close()
        return "data:image/%s;base64,%s" % (image_type, base64.b64encode(data))
    except IOError:
        return path1